import type { RouteRecordRaw, Router } from 'vue-router'
/** 对菜单参数进行处理，生成合理的路由 */
const initRoute = (routerArr: RouteRecordRaw[]) => {
  const routes: { path: string; title: string; children: any[] }[] = []
  routerArr.forEach((topRoute) => {
    if (topRoute.meta?.hideInMenu !== undefined) {
      if (!topRoute.meta?.hideInMenu) {
        const currentRoute: {
          path: string
          title: string
          children: Array<any>
        } = {
          path: '',
          title: '',
          children: []
        }
        currentRoute.path = topRoute.path
        currentRoute.title = topRoute.meta?.title as string
        currentRoute.children = topRoute.children
          ? (initRoute(topRoute.children) as unknown as Array<any>)
          : []
        routes.push(currentRoute)
      }
    }
  })
  return routes
}
const setupMenu = (router: Router) => {
  return initRoute(
    router
      .getRoutes()
      .filter((item) => !item.name?.toString().includes('-') && !item.meta.hideInMenu)[0].children
  )
}

export default setupMenu
